/*
 * UserPageTitledDisplayLinks.java
 *
 * Display Labels and Links
 */

package net.kernub.client;

import com.google.gwt.core.client.GWT;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;

import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.FlowPanel;

import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.DialogBox;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyCodes;

import java.lang.Integer;
import java.util.ArrayList;

import net.kernub.client.core.UpdateHandler;
import net.kernub.client.core.RPC;
import net.kernub.client.core.RPCCallback;
import net.kernub.client.core.datatype.CInteger;
import net.kernub.client.core.datatype.CBoolean;
import net.kernub.client.core.datatype.CString;

import net.kernub.client.wwwConstants;
import net.kernub.client.PageTitled;
import net.kernub.client.LabelData;
import net.kernub.client.LinkData;
import net.kernub.client.UIGlobals;

import net.kernub.client.datatype.LinkDesc;
import net.kernub.client.datatype.LinkDescArray;
import net.kernub.client.datatype.LabelDesc;
import net.kernub.client.datatype.LabelDescArray;


public class UserPageTitledDisplayLinks extends PageTitled
{
	private final wwwConstants constants = GWT.create( wwwConstants.class ); 

	private final VerticalPanel sysLabelsPanel = new VerticalPanel();
	private final VerticalPanel labelsPanel = new VerticalPanel();
	private final FlowPanel mainPanel = new FlowPanel();

	private final ArrayList<LabelData> labelsList = new ArrayList<LabelData>();
	private final ArrayList<LinkData> linksList = new ArrayList<LinkData>();

	private LabelBoard selectedLabelBoard = null;

	public UserPageTitledDisplayLinks()
	{
		labelsPanel.setSpacing( 4 );

		sysLabelsPanel.setSpacing( 4 );

		final Label labelsTitleLabel = new Label( constants.linkLabelListTitle() );
		labelsTitleLabel.addStyleName( "PageText" );
		labelsTitleLabel.addStyleName( "LabelListTitle" );

		final VerticalPanel labelsPanel = new VerticalPanel();
		labelsPanel.setSpacing( 0 );
		labelsPanel.add( this.sysLabelsPanel );
		labelsPanel.add( labelsTitleLabel );
		labelsPanel.add( this.labelsPanel );

		final VerticalPanel rootMainPanel = new VerticalPanel();
		rootMainPanel.setSpacing( 0 );
		rootMainPanel.setWidth( "100%" );
		rootMainPanel.add( mainPanel );
		rootMainPanel.setCellHorizontalAlignment( mainPanel, VerticalPanel.ALIGN_LEFT );

		final HorizontalPanel panel = new HorizontalPanel();
		panel.setWidth( "100%" );
		panel.add( labelsPanel );
		panel.add( rootMainPanel );
		panel.setVerticalAlignment( HorizontalPanel.ALIGN_TOP );
	
		createSystemLabels();

		setContent( panel );

		final Anchor aboutAnchor = new Anchor( constants.linkAbout(), false );
		aboutAnchor.addStyleName( "PageText" );
		aboutAnchor.addClickHandler( new ClickHandler() {
			public void onClick( ClickEvent event )
			{
				www.instance().setPage( www.PAGE_ABOUT );
			}
		});

		sysLabelsPanel.add( aboutAnchor );

		final Anchor addNewLinkAnchor = new Anchor( constants.linkAddNewLink(), false );
		addNewLinkAnchor.addStyleName( "PageText" );
		addNewLinkAnchor.addStyleName( "NewLinkAnchor" );
		addNewLinkAnchor.addClickHandler( new ClickHandler() {
			public void onClick( ClickEvent event )
			{
				www.instance().setPage( www.PAGE_ADDEDIT_LINK );
			}
		});

		final Anchor editHomeAnchor = new Anchor( constants.linkEditHome(), false );
		editHomeAnchor.addStyleName( "SystemLabel_Little" );
		editHomeAnchor.setWidth( LabelBoard.PANEL_WIDTH );
		editHomeAnchor.addClickHandler( new ClickHandler() {
			public void onClick( ClickEvent event )
			{
				www.instance().setPage( www.PAGE_EDITHOME );
			}
		});

		final Anchor adminLabelAnchor = new Anchor( constants.linkAdminLabels(), false );
		adminLabelAnchor.addStyleName( "SystemLabel_Little" );
		adminLabelAnchor.setWidth( LabelBoard.PANEL_WIDTH );
		adminLabelAnchor.addClickHandler( new ClickHandler() {
			public void onClick( ClickEvent event )
			{
				www.instance().setPage( www.PAGE_MANAGE_LABEL );
			}
		});

		UIGlobals.instance().addUpdateHandler( new UpdateHandler() {
			public void onUpdate()
			{
				if( UIGlobals.instance().getLinkAddable() )
				{
					rootMainPanel.insert( addNewLinkAnchor, 0 );
					rootMainPanel.setCellHorizontalAlignment( addNewLinkAnchor, VerticalPanel.ALIGN_RIGHT );
				}
				else
				{
					rootMainPanel.setCellHorizontalAlignment( addNewLinkAnchor, VerticalPanel.ALIGN_LEFT );
					rootMainPanel.remove( addNewLinkAnchor );
				}

				if( UIGlobals.instance().getLabelManagable() )
				{
					sysLabelsPanel.add( adminLabelAnchor );
				}
				else
				{
					sysLabelsPanel.remove( adminLabelAnchor );
				}

				if( UIGlobals.instance().getHomeEditable() )
				{
					sysLabelsPanel.add( editHomeAnchor );
				}
				else
				{
					sysLabelsPanel.remove( editHomeAnchor );
				}
			}
		});
	}

	public void setActive()
	{
		super.setActive();

		setSmallTitle( constants.linkDisplayMainTitle() );

		updateHome();

		updateLabels();
	}

	public void createSystemLabels()
	{
		final Label homeLabel = new Label( constants.linkHome() );
		homeLabel.addStyleName( "LabelBoard" );
		homeLabel.addStyleName( "SystemLabel" );
		homeLabel.addStyleName( "LinkCursor" );
		homeLabel.setWidth( LabelBoard.PANEL_WIDTH );
		homeLabel.addClickHandler( new ClickHandler() {
			public void onClick( ClickEvent event )
			{
				updateHome();

				if( selectedLabelBoard != null )
					selectedLabelBoard.selected( false );
			}
		});

		sysLabelsPanel.add( homeLabel );
	}

	public void updateHome()
	{
		DeferredCommand.addCommand( new Command() {
			public void execute()
			{
				RPC.call( "site/Home", "getText", new RPCCallback<CString>() {
					public void onCallback( CString r )
					{
						linksList.clear();
						mainPanel.clear();

						final HTML html = new HTML( r.get() );
						mainPanel.add( html );
					}
				});
			}
		});
	}

	public void updateLabels()
	{
		labelsList.clear();
		labelsPanel.clear();

		DeferredCommand.addCommand( new Command() {
			public void execute()
			{
				RPC.call( "site/Label", "getList", new RPCCallback<LabelDescArray>() {
					public void onCallback( LabelDescArray r )
					{
						for( int i = 0; i < r.labels().length(); i++ )
						{
							final LabelData label = new LabelData( r.labels().get(i).getId(), r.labels().get(i).getName() );
							final LabelBoard board = new LabelBoard( label.getID(), label.getName() );

							board.addClickHandler( new ClickHandler() {
								public void onClick( ClickEvent event )
								{
									if( selectedLabelBoard != null )
										selectedLabelBoard.selected( false );
									selectedLabelBoard = board;
									board.selected( true );

									updateLinksList( label.getID() );
								}
							});

							labelsList.add( label );
							labelsPanel.add( board );
						}

						final LabelBoard board = new LabelBoard( 0, constants.linkUnlabelled() );
						board.addClickHandler( new ClickHandler() {
							public void onClick( ClickEvent event )
							{
								if( selectedLabelBoard != null )
									selectedLabelBoard.selected( false );
								selectedLabelBoard = board;
								board.selected( true );

								updateLinksUnlabelledList();
							}
						});

						labelsPanel.add( board );
					}
				});
			}
		});
	}

	public void updateLinksList( final int labelID )
	{
		DeferredCommand.addCommand( new Command() {
			public void execute()
			{
				RPC.call( "site/Link", "getList", labelID, new RPCCallback<LinkDescArray>() {
					public void onCallback( LinkDescArray r )
					{
						linksList.clear();
						mainPanel.clear();

						updateLinksFromDesc( r );
					}
				});
			}
		});
	}

	public void updateLinksUnlabelledList()
	{
		DeferredCommand.addCommand( new Command() {
			public void execute()
			{
				RPC.call( "site/Link", "getUnlabelledList", new RPCCallback<LinkDescArray>() {
					public void onCallback( LinkDescArray r )
					{
						linksList.clear();
						mainPanel.clear();

						updateLinksFromDesc( r );
					}
				});
			}
		});
	}

	public void updateLinksFromDesc( final LinkDescArray r )
	{
		if( r.links().length() == 0 )
		{
			final Label l = new Label( constants.linkNoLinks() );
			l.addStyleName( "PageText" );
			
			mainPanel.add( l );
			return;
		}

		for( int i = 0; i < r.links().length(); i++ )
		{
			LinkDesc ld = r.links().get( i );
			final LinkData link = new LinkData( ld.getID(), ld.getName(), ld.getURL(), ld.getDescription() );
			final LinkBoard board = new LinkBoard( ld.getID(), ld.getName(), ld.getURL(), ld.getDescription() );

			linksList.add( link );
			mainPanel.add( board );
		}
	}

};

